<?php 
	include '../init.php';

	$bz = $_GET['bz'];

	switch ($bz){
		case 'cart':
		// var_dump($_GET);exit;

		$id = $_GET['gid'];
		$count = $_GET['count'];
		$stock = $_GET['stock'];
		if($count <=0 ){
			notice('不能为负数..');
		}
		if($count > $stock){
			notice('库存不足..');
		}

		$sql = 'select g.id, g.name, g.price, g.sales, g.stock, i.icon
				from goods g ,goodsImg i
				where g.id = i.gid and face = 1 and g.id = '.$id;
		// echo $sql ;exit;

		$goods_list = query($sql)[0];
		$goods_list['count'] = $count;

		$_SESSION['cart'][$id] = $goods_list;
		// var_dump($_SESSION['cart']);
		// exit;

		header('location:shopcart.php');
			break;
		
		case 'jian':
			$id = $_GET['id'];
			$_SESSION['cart'][$id]['count']--;

			if($_SESSION['cart'][$id]['count'] < 1){
				$_SESSION['cart'][$id]['count'] = 1;
			}

			header('location:'.$_SERVER['HTTP_REFERER']);

			break;

		case 'jia':
			$id = $_GET['id'];
			$_SESSION['cart'][$id]['count']++;

			if($_SESSION['cart'][$id]['count'] > $_SESSION['cart'][$id]['stock']){
				$_SESSION['cart'][$id]['count'] = $_SESSION['cart'][$id]['stock'];
			}

			header('location:'.$_SERVER['HTTP_REFERER']);

			break;

		case 'del':
			unset($_SESSION['cart'][$_GET['id']]);
			header('location:'.$_SERVER['HTTP_REFERER']);
			break;


		case 'orderinfo':
			if(empty($_SESSION['home'])){
				notice('您尚未登录,请先登录,,','../login/login.php');
			}

			header('location:address.php');


			break;



		case 'address':
			

			// if(empty($_SESSION['home'])){
			// 	notice('您尚未登录,请先登录,,','../login/login.php');
			// }
			// uuid  订单编号 
			$chars = md5(uniqid(mt_rand(), true));  
		    $uuid  = substr($chars,0,8) . '-';  
		    $uuid .= substr($chars,8,4) . '-';  
		    $uuid .= substr($chars,12,4) . '-';  
		    $uuid .= substr($chars,16,4) . '-';  
		    $uuid .= substr($chars,20,12);  
		   
		    // $add_list = $_POST;
			
		    // foreach ($add_list as $v) {
		    // 	if(!empty($v['receiver']));
		    // }
			$add_list = $_POST;
			
			if(empty($add_list['receiver']&&$add_list['phone']&&$add_list['address'])){
			notice('收信息不能为空');  
			}	
               $preg = '/^1[34578]\d{9}/';
               $str=$add_list['phone'];
               if( preg_match($preg, $str, $result)  ){

	    
	}else{
		    notice('请输入正确的号码,请重新输入');
	}



			// if(empty($add_list['phone'])){
			// 	notice('收货人电话不能为空');  
			// 	}
			// if(empty($add_list['address'])){
			// 	notice('收货人地址不能为空');  
			// 	}

			// var_dump($add_list);exit;

			$add_list['orderNum'] = $uuid;
			
			$uid = $_SESSION['home']['id'];
			$add_list['uid'] = $uid;
			

			// var_dump($add_list);
			$field_key ='';
			$field_value ='';


			foreach($add_list as $k => $v){
				
				$field_key .= ' `'.$k.'`, ';
				$field_value .=  '"'.htmlentities($v).'",';
			}
			// var_dump($field_key);
			// var_dump($field_value);
			$field_key .= '`id`,`time`,`status`,`isPay`';
			// var_dump($field_key);

			$field_value .= 'NULL,UNIX_TIMESTAMP(),DEFAULT,DEFAULT';
			// var_dump($field_value);exit;
			
			$sql ='insert into orders('.$field_key.')
				 values('.$field_value.')';
			// var_dump($sql);exit;
			
			$result = zsg($sql);
			// var_dump($result);

			if($result){
				foreach($_SESSION['cart'] as $v){
					$sql = 'insert into ordersGood values(null,"'.$result.'","'.$v['id'].'","'.$v['price'].'","'.$v['count'].'")';

					zsg($sql);
					}
						unset($_SESSION['cart']);
						notice('付款成功..','success.php');
					}else{
						notice(' 付款失败..');
					}	
				
			break;

			case 'success':

			break;

		
		default:
			# code...
			break;		

	}
 ?>